﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Operations
{
	internal class InsertBone : Operation
	{
        Engine.Selection Selected;
		Vector2 Point;
        Data.Skeleton Skeleton;
        int ParentBone;

        internal InsertBone(Engine.Selection Selected, Vector2 Point)
		{
			this.Selected = Selected;
			this.Point = Point;
            Skeleton = null;
		}

        internal override ReturnCode Do(EditorState ES)
        {
            Skeleton = ES.GetMiddleObject(ES.Selected) as Data.Skeleton;
            if (Skeleton == null) return ReturnCode.DoNotAddToUndoStack;
            if (Selected.Type != Jemgine.Engine.Selection.Types.Vertex) ParentBone = 0;
            else ParentBone = Selected.BottomIndex;
            var Bone = new Data.Bone(Point);
            Bone.Parent = ParentBone;
            Skeleton.AddBone(Bone);
            ES.SelectItem(new Engine.Selection(Engine.Selection.Types.Vertex, Selected.TopIndex, Selected.MiddleIndex,
                Skeleton.Count - 1));
            return ReturnCode.AddToUndoStack;
        }

        internal override void Undo(EditorState ES)
        {
            Skeleton.RemoveBone(Skeleton.Count - 1);
        }
	}
}
